﻿#include "BrowserTableHandlerMaterialFluid.h"
#include "OperatorsInterface/OperAbstractMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFluid.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QPushButton>
#include <QColorDialog>

namespace GUI
{

    void BrowserTableHandlerMaterialFluid::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Speed of Sound [m/s]") << tr("Density [kg/m3]") <<
            tr("Viscosity [m2/s]") << tr("Color") << tr("Notes");
       
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerMaterialFluid::updateData()
    {
        if (_tableWidget == nullptr) return;

        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        //获取材料管理器
        Acoustics::FITKAcousticsMaterialManager* matmgr = phys->getMaterialManager();
        if (matmgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsMaterial*> matList = matmgr->getAcousticsMaterials(
            Acoustics::FITKAcousticsAbsMaterial::Fluid);
        //材料数量
        const int n = matList.size();
        _tableWidget->setRowCount(n);

        for (int i = 0 ;i< n; ++i)
        {
            //转换为流体材料
            Acoustics::FITKAcousticsMaterialsFluid* maf = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>
                (matList.at(i));
            if(maf == nullptr) continue;
            //数据ID
            const int id = maf->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(maf->getDataObjectName(),id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(maf->getSpeedOfSound()), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(maf->getDensity()), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(maf->getKinematicViscosity()), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(maf->getMaterialDescribe(), id));
            
            float color[3] = { 0,0,0 };
            maf->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 4, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button,maf); });            
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractMaterial* BrowserTableHandlerMaterialFluid::getMaterialOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractMaterial>("OperMaterialFluid");
    }

    bool BrowserTableHandlerMaterialFluid::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //只有密度能批量
        if (colIndex !=2) return false;

        return true;
    }


    void BrowserTableHandlerMaterialFluid::onEditItemColor(QPushButton* button, Acoustics::FITKAcousticsMaterialsFluid* mat)
    {
        if (button == nullptr || mat == nullptr) return;
        //修改之前的颜色
        float c[3] = { 0,0,0 };
        mat->getColor(c);
        QColor oriC(c[0] * 255, c[1] * 255, c[2] * 255);
        //颜色选择
        QColor color = QColorDialog::getColor(oriC, _tableWidget, tr("Select Fluid Color"));
        if (!color.isValid()) return;
        //修改颜色
        mat->setColor(color.redF(), color.greenF(), color.blueF());
        this->SetPushButtonColor(button, color.red(), color.green(), color.blue());
        //刷新渲染
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateAll();
    }

}




